package io.wdb.modules.generator.generator.controller.book;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;

import io.wdb.common.utils.PageUtils;
import io.wdb.common.utils.Query;
import io.wdb.common.utils.R;
import io.wdb.modules.generator.generator.controller.api.BaseController;
import io.wdb.modules.generator.generator.entity.customer.BookEntityCustomer;
import io.wdb.modules.generator.generator.entity.customer.BookInfoEntityCustomer;
import io.wdb.modules.generator.generator.entity.web.BaseResponse;
import io.wdb.modules.generator.generator.entity.web.WebResponse;
import io.wdb.modules.generator.generator.service.book.BookContentService;

@RestController
@RequestMapping("book/content")
public class BookContentController extends BaseController{
	private Logger logger = LoggerFactory.getLogger(BookContentController.class);
	
	@Autowired
	private BookContentService bookContentService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	//@RequiresPermissions("accountcash:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        List<BookInfoEntityCustomer> bookInfoEntityCustomers = bookContentService.list(query);
        PageInfo<BookInfoEntityCustomer> page = new PageInfo<BookInfoEntityCustomer>(bookInfoEntityCustomers);
		int total = (int) page.getTotal();
		PageUtils pageUtil = new PageUtils(bookInfoEntityCustomers, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	/**
	 * 
	 * @Method:getInfo
	 * @Description:获取某一本书的所有信息，进而编辑
	 * @Time 2017年12月26日 下午2:14:37
	 * @author: Tom
	 * @param id
	 * @return
	 */
	@RequestMapping("/info/{isbn}")
	public BaseResponse getInfo(@PathVariable("isbn") String isbn){
		BaseResponse baseResponse = success();
		BookEntityCustomer bookEntityCustomer = null;
		try {
			bookEntityCustomer = bookContentService.getInfoByIsbn(isbn);
			baseResponse.setDetail(bookEntityCustomer);
		} catch (Exception e) {
			baseResponse = failed(e);
			logger.error(e.getMessage(),e);
		}
		return baseResponse;
	}
	/**
	 * 
	 * @Method:updateInfo
	 * @Description:修改信息
	 * @Time 2017年12月26日 下午3:55:11
	 * @author: Tom
	 * @param bookEntityCustomer
	 * @return
	 */
	@RequestMapping("/updateInfo")
	public BaseResponse updateInfo(@RequestBody BookEntityCustomer bookEntityCustomer){
		BaseResponse baseResponse = success();
		try {
			WebResponse webResponse = bookContentService.updateInfo(bookEntityCustomer);
			baseResponse.setDetail(webResponse);
		} catch (Exception e) {
			baseResponse = failed(e);
			logger.error(e.getMessage(),e);
		}
		return baseResponse;
	}
}
